/**
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <algorithm>
#include <iostream>
#include <ostream>
#include <string>
#include <cstring>

#include "os/library_loader.h"

#include "public/es2panda_lib.h"
#include "util.h"

/*
 * Covered C API List:
 *   AllocMemory
 *   CreateConfig
 *   CreateContextFromString
 *   CreateNumberType
 *   CreateStringLiteralType
 *   CreateStringType
 *   CreateUnionType
 *   DestroyConfig
 *   ProceedToState
 *   TypeIsStringLiteralType
 *   TypeIsUnionType
 *   TypeToStringConst
 */

// NOLINTBEGIN
static std::string source = R"(
)";
static es2panda_Impl *impl = nullptr;
static es2panda_Context *context = nullptr;

static int CheckUnionType()
{
    es2panda_Type *stringType = impl->CreateStringType(context);
    es2panda_Type *intType = impl->CreateNumberType(context);
    es2panda_Type **types = static_cast<es2panda_Type **>(impl->AllocMemory(context, 2, sizeof(es2panda_Type *)));
    types[0] = stringType;
    types[1] = intType;
    es2panda_Type *unionType = impl->CreateUnionType(context, types, 2);
    if (!impl->TypeIsUnionType(unionType)) {
        return TEST_ERROR_CODE;
    }
    return 0;
}

static int CheckStringLiteralType()
{
    es2panda_Type *stringLiteralType = impl->CreateStringLiteralType(context, const_cast<char *>("test"));
    if (!impl->TypeIsStringLiteralType(stringLiteralType)) {
        return TEST_ERROR_CODE;
    }
    return 0;
}

int main(int argc, char **argv)
{
    if (argc < MIN_ARGC) {
        return INVALID_ARGC_ERROR_CODE;
    }

    if (GetImpl() == nullptr) {
        return NULLPTR_IMPL_ERROR_CODE;
    }
    impl = GetImpl();

    const char **args = const_cast<const char **>(&(argv[1]));
    auto config = impl->CreateConfig(argc - 1, args);
    context = impl->CreateContextFromString(config, source.data(), argv[argc - 1]);
    if (context == nullptr) {
        std::cerr << "FAILED TO CREATE CONTEXT" << std::endl;
        return NULLPTR_CONTEXT_ERROR_CODE;
    }
    int code = 0;
    code = CheckUnionType();
    if (code != 0) {
        std::cerr << "FAILED TO CHECK UNION TYPE" << std::endl;
        return code;
    }
    code = CheckStringLiteralType();
    if (code != 0) {
        std::cerr << "FAILED TO CHECK STRING LITERAL TYPE" << std::endl;
        return code;
    }

    impl->ProceedToState(context, ES2PANDA_STATE_CHECKED);
    CheckForErrors("CHECKED", context);

    auto Ast = impl->ProgramAst(context, impl->ContextProgram(context));
    impl->AstNodeRecheck(context, Ast);

    impl->DestroyConfig(config);
    return 0;
}

// NOLINTEND
